<?php
class GoodsController extends AppController {

	public $name = 'goods';
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','lockgoods','edit','categorieslist','categoriesadd','categoriesedit','categoriesdel','getcatbrandlist','attributelist','attributeedit','attributedel','attributeextend');

	}
	
	public function lockgoods() {
        $value = intval(Request::get_args('v'));
        $goodsid=Request::get_args('goods_id');
        if($goodsid){
	        $goods_id=$goodsid;
        }else{
	        $goods_id = intval(Request::get_args('id'));
        }
        if(!$goods_id) {exit('非法操作');}
		$query = array(
			'conditions' =>array('goods.id' => $goods_id), 
			'fields' => array('goods.shop_id','goods.goods_name'), 
		);
		$goods_userinfo = POP::exe('[@@:UM goods][@@:QU find:first]',$query);			
		$goods_userinfo = $goods_userinfo['goods'];
        $shop_id = $goods_userinfo['shop_id'];
        $goods_name = $goods_userinfo['goods_name'];
        //锁定商品
		$query = array();
		$query['keyid'] = true;
		$safeData['goods'] = array('lock_flg'=>$value);				
		$query['set'] = $safeData;
		$query['conditions'] = array('id' => $goods_id);				
		$update = POP::exe('[@@:UM goods][@@:QU save]',$query);
		
        if($value==1){
	        if($update) {
		        // 发送提醒
		        $remind_id = 4;
		        $user_id = $shop_id;
		        // 获取提醒信息
				$query = array(
					'conditions' => array('remind.id' => $remind_id), 
					'fields' => array('remind.*'), 
				);
				$reminds = POP::exe('[@@:UM remind][@@:QU find:first]',$query);					
				$reminds = $reminds['remind'];
		        if($reminds['enable']) {
			        $remind_tpl = $reminds['remind_tpl'];
			        // 获取用户的提醒设置
					$query = array(
						'conditions' => array('reminduser.user_id' => $user_id,'reminduser.remind_id' => $remind_id), 
						'fields' => array('reminduser.*'), 
					);
					$row = POP::exe('[@@:UM reminduser][@@:QU find:first]',$query);						
					
			        if($row) {
					    $row = $row['reminduser'];
				        $nowtime = date("Y-m-d H:i:s");	
				        $remind_info = remind_info_replace($remind_tpl,array('time'=>$nowtime,'goods'=>$goods_name));
				        if($row['site']) {
					        $array = array(
						    'user_id' => $user_id,
						    'remind_info' => $remind_info,
						    'remind_time' => $nowtime,
					        );
					        if($reminds['enable']==1){
	                            $safeData['remindinfo'] = $array;								
				                POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);						
					        }
				        }
				        if($row['mail']) {
						    //获得用户信息
							$query = array(
								'conditions' => array('user.id' => $user_id), 
								'fields' => array('user.*'), 
							);
							$user_info = POP::exe('[@@:UM user][@@:QU find:first]',$query);								
						    $user_info = $user_info['user'];
						    $user_email = $user_info['email'];
						    $user_name = $user_info['username'];
						    //邮件发送
						    $nowtime = date("Y-m-d H:i:s");	
						    $mailbody = "亲爱的用户".$user_name."您好，"."<br />"."您的商品:".$goods_name."于".$nowtime."被管理员锁定了。"."<br />如有疑问请联系管理员，感谢您对本站的支持。<br />( 这是系统自动产生的邮件，请勿回复。)"."<br />";
						    $mailtitle = "邮箱提醒";
	                        $this->_lockgoodsConfirmEmail($user_email,$user_name,$mailtitle,$mailbody);
				        }
				        if($row['im']) {

				        }
				        if($row['mobile']) {

				        }
			        }
		        }
				$array = array(
				    'log_time' => date("Y-m-d H:i:s"),
				    'admin_id' => $this->my->id,
					'admin_name' => $this->my->username,
				    'log_info' => "锁定商品"."：$goods_id",
					'ip_address' => Request::getclientIp(),					
			    );
	            $safeData['adminlog'] = $array;								
				POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);					
                $this->flash("商品锁定成功", Router::url('admin/goods/index'));		
	        } else {
		        $this->flash("商品锁定失败", Router::url('admin/goods/index'));	
	        }
        }else{
	        if($update) {
		        // 发送提醒
		        $remind_id = 7;
		        $user_id = $shop_id;
		        // 获取提醒信息
				$query = array(
					'conditions' => array('remind.id' => $remind_id), 
					'fields' => array('remind.*'), 
				);
				$reminds = POP::exe('[@@:UM remind][@@:QU find:first]',$query);	
				$reminds = $reminds['remind'];
		        if($reminds['enable']) {
			        $remind_tpl = $reminds['remind_tpl'];
			        // 获取用户的提醒设置
					$query = array(
						'conditions' => array('reminduser.user_id' => $user_id,'reminduser.remind_id' => $remind_id), 
						'fields' => array('reminduser.*'), 
					);
					$row = POP::exe('[@@:UM reminduser][@@:QU find:first]',$query);						
			        if($row) {
					    $row = $row['reminduser'];					
				        $nowtime = date("Y-m-d H:i:s");	
				        $remind_info = remind_info_replace($remind_tpl,array('time'=>$nowtime,'goods'=>$goods_name));
				        if($row['site']) {
					        $array = array(
						    'user_id' => $user_id,
						    'remind_info' => $remind_info,
						    'remind_time' => $nowtime,
					        );
					        if($reminds['enable']==1){
	                            $safeData['remindinfo'] = $array;								
				                POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);						
					        }
				        }						
				        if($row['mail']) {
						    //获得用户信息
							$query = array(
								'conditions' => array('user.id' => $user_id), 
								'fields' => array('user.*'), 
							);
							$user_info = POP::exe('[@@:UM user][@@:QU find:first]',$query);								
						    $user_info = $user_info['user'];
						    $user_email = $user_info['email'];
						    $user_name = $user_info['username'];
						    //邮件发送
						    $nowtime = date("Y-m-d H:i:s");	
						    $mailbody = "亲爱的用户".$user_name."您好，"."<br />"."您的商品:".$goods_name."于".$nowtime."被管理员解除锁定了。"."<br />如有疑问请联系管理员，感谢您对本站的支持。<br />( 这是系统自动产生的邮件，请勿回复。)"."<br />";
						    $mailtitle = "邮箱提醒";
	                        $this->_lockgoodsConfirmEmail($user_email,$user_name,$mailtitle,$mailbody);
				        }						
				        if($row['im']) {

				        }
				        if($row['mobile']) {

				        }
			        }
		        }
				$array = array(
				    'log_time' => date("Y-m-d H:i:s"),
				    'admin_id' => $this->my->id,
					'admin_name' => $this->my->username,
				    'log_info' => "解锁商品"."：$goods_id",
					'ip_address' => Request::getclientIp(),					
			    );
	            $safeData['adminlog'] = $array;									
				POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);				
                $this->flash("商品解除锁定成功", Router::url('admin/goods/index'));					
	        } else {
                $this->flash("商品解除锁定失败", Router::url('admin/goods/index'));	
	        }
        }	
    }	
	
	function _lockgoodsConfirmEmail($addr, $username, $mailtitle ,$mailbody) {
		$this->Email->to = $addr;
		$this->Email->bcc = array('noreply@ggl.com');  
		$this->Email->subject = $mailtitle;
		$this->Email->replyTo = 'noreply@ggl.com';
		$this->Email->from = '"GGL" <noreply@ggl.com>';
		$this->Email->template = 'confirm_getpasswd'; // note no '.ctp'
		//Send as 'html', 'text' or 'both' (default is 'text')
		$this->Email->sendAs = 'both'; // because we like to send pretty mail
		//Set view variables as normal
		$this->set('user', $username);
		$this->set('ConfirmationCode', $mailbody);
		
		//------ 采用SMTP方式发送邮件
		//SMTP Options
		$this->Email->smtpOptions = array(
			'port'=>'465', 
			'timeout'=>'30',
			'host' => 'ssl://smtp.gmail.com',
			'username'=>'noreply@ggl.com',
			'password'=>'zxcvl#8v',
		);

		//Set delivery method 
		//使用SMTP方式需要确认的是：/cake/libs/controller/components/email.php 中的 HELO 修改为 EHLO (1.3版本不再需要修改)
		//email.php更改成var $xMailer = 'GGL Email';
		$this->Email->delivery = 'smtp';
		//------ SMTP设置完成，如要使用PHP函数发送邮件，则直接注释该部分		
		//Do not pass any args to send()
		$this->Email->send();	
	}	
	
	public function edit($goods_id) {
        $goods_id = intval($goods_id);	
		if (!empty($this->data)) {
			$query = array(
				'conditions' =>array('goods.id' => $goods_id,'goods.shop_id' => $this->data['shop_id']), 
				'fields' => array('goods.*'), 
			);
			$info = POP::exe('[@@:UM goods][@@:QU find:first]',$query);		
			if($info) {
			    $info = $info['goods'];
	            $nowtime = date("Y-m-d H:i:s");
				$shop_id = intval($this->data['shop_id']);			
	            if(intval($this->data['lock_flg'])){
		            if($info['lock_flg']!=intval($this->data['lock_flg'])){
			            $lock = "商品被锁定";
			            if(intval($this->data['lock_flg'])==0){
				            $lock = "商品被解锁";
			            }
			            $array = array(
				            'user_id' => $shop_id,
				            'remind_info' => "在".$nowtime."，".$post['goods_name'].$lock,
				            'remind_time' => $nowtime,
			            );
	                    $safeData['remindinfo'] = $array;
					    POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);													
		            }
	            }
	            if($this->data['goods_name']){
		            if($info['goods_name']!=$this->data['goods_name']){
			            $array = array(
				        'user_id' => $shop_id,
				        'remind_info' => "在".$nowtime."，".$info['goods_name']."商品被改名",
				        'remind_time' => $nowtime,
			            );
	                    $safeData['remindinfo'] = $array;								
					    POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
		            }
	            }
	            if($this->data['goods_intro']){
		            if($info['goods_intro']!=$this->data['goods_intro']){
			            $array = array(
				            'user_id' => $shop_id,
				            'remind_info' => "在".$nowtime."，".$info['goods_name']."商品简介被修改",
				            'remind_time' => $nowtime,
			            );
	                    $safeData['remindinfo'] = $array;								
					    POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
		            }
	            }	
			}
            $safeData['goods']['lock_flg'] = $this->data['lock_flg'];
            $safeData['goods']['goods_name'] = $this->data['goods_name'];
            $safeData['goods']['goods_intro'] = $this->data['goods_intro'];			
			$query['keyid'] = $goods_id;
			$query['set'] = $safeData;						
			if (POP::exe('[@@:UM goods][@@:QU save]',$query)) {
			    $this->flash('更新成功！', Router::url('admin/goods/index'));		
			} else {
                $this->flash('更新失败！', Router::url('admin/goods/index'));
			}
	    }else {
			$query = array(
				'conditions' =>array('goods.id' => $goods_id), 
				'fields' => array('goods.*'), 
			);
			$info = POP::exe('[@@:UM goods][@@:QU find:first]',$query);				
            $info = $info['goods'];
		    $this->set('info',$info);
        }			
    }	
	
	public function index() {	
        $page=Request::get_args("page");	
        $name = Request::get_args('name');
        $best = intval(Request::get_args('best'));
        $new = intval(Request::get_args('new'));
        $hot = intval(Request::get_args('hot'));
        $promote = intval(Request::get_args('promote'));
        $admin_promote = intval(Request::get_args('admin_promote'));
        $sale = intval(Request::get_args('sale'));
        $shop_id = intval(Request::get_args('shopid'));	
	
        $conditions = array();
        if($name) {
		    $conditions['goods_name like']= $name;
	    }
        if($best) {
		    $conditions['is_best']= 1;		
        }
        if($new) {
		    $conditions['is_new']= 1;		
        }
        if($hot) {
		    $conditions['is_hot']= 1;			
        }
        if($promote) {
		    $conditions['is_promote']= 1;			
        }
        if($admin_promote) {
		    $conditions['is_admin_promote']= 1;			
        }
        if($sale) {
		    $conditions['is_on_sale']= 1;			
        }
        if ($shop_id){
		    $conditions['shop_id']= $shop_id;			
        }
		
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'goods',
				    'fields' => array('goods.*'),
					'conditions' => $conditions,
					'order' => ' add_time desc ',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 		
		$this->set('result',$result);	
	}
	
	public function attributelist() {
        $inputtype_arr = array(
	        '0'	=> '文本框输入(text)',
	        '1' => '下拉框选择(select)',
	        '2' => '单选框选择(radio)',
	        '3' => '多选框选择(checkbox)',
        );	
	    $cat_id = 0 ;
	    if(isset(Request::$query['cat_id'])){
		    $cat_id = intval(Request::$query['cat_id']);
		} 
		
		$query = array(
			'order' => ' sort_order asc,id asc ', 
			'fields' => array('goodscategories.*'), 
		);
		$result_category = POP::exe('[@@:UM goodscategories][@@:QU find:all]',$query);	
		$category_dg = $this->get_dg_category($result_category);
		
        // 当前选中的分类信息
        $parent_id = 0;
        $cat_info = array();
        $attr_info = array();
        if($cat_id) {
	        foreach($result_category as $value) {
		        if($value['goodscategories']['id'] == $cat_id) {
			        $cat_info = $value['goodscategories'];
			        break;
		        }
	        }
	        if($cat_info) {
				$query = array(
					'conditions' => array('attribute.cat_id' => $cat_id), 
					'fields' => array('attribute.*'), 
				);
				$attr_info = POP::exe('[@@:UM attribute][@@:QU find:all]',$query);				
		        $parent_id = $cat_info['parent_id'];
				$this->set('parent_id',$parent_id);	
		        $this->set('attr_info',$attr_info);
	        }
        }			
        $this->set('category_dg',$category_dg);	
		$this->set('cat_id',$cat_id);	
		$this->set('inputtype_arr',$inputtype_arr);		
	}	

	public function attributeedit() {
        $this->data['cat_id'] = intval(Request::$data['cat_id']);
        $this->data['attr_name'] = Request::$data['attr_name'];
        $this->data['input_type'] = intval(Request::$data['input_type']);
        $this->data['attr_values'] =Request::$data['attr_values'];
        $this->data['sort_order'] = intval(Request::$data['sort_order']);
        $attr_id = intval(Request::$data['attr_id']);
        if(!$this->data['cat_id']) {exit("-1");}
        if(empty($this->data['attr_name'])) { exit("-1"); }
        if($attr_id>0) {
			$query['keyid'] = $attr_id;
			$safeData['attribute'] = $this->data;				
			$query['set'] = $safeData;			
			if(POP::exe('[@@:UM attribute][@@:QU save]',$query)) {			
		        echo $attr_id;
	        } else {
		        echo "-1";
	        }
        } else {		
			$safeData['attribute'] = $this->data;
			$query = $safeData;
			$new_attr_id = POP::exe('[@@:UM attribute][@@:QU save]',$query);
			if ($new_attr_id) {
		        echo $new_attr_id;
	        } else {
                echo "-1";
	        }
        }
        exit;		
	}	
	public function attributedel() {
        $id = intval(Request::$data['id']);
        if(!$id) {
	        exit("-1");
        }
		$query = array('keyid'=>$id);
        if(POP::exe('[@@:UM attribute][@@:QU del]',$query)) {
	        echo "1";
        } else {
	        echo "-1";
        }
        exit;		
	}	
	public function attributeextend() {
        $cat_id = intval(Request::$data['cat_id']);
        $parent_id = intval(Request::$data['parent_id']);
        if(!$cat_id || !$parent_id) {exit("-1");}		
		$query = array(
			'conditions' => array('OR' => array('attribute.cat_id' => $cat_id, 'attribute.cat_id' => $parent_id)), 
			'fields' => array('attribute.*'), 
		);
		$result = POP::exe('[@@:UM attribute][@@:QU find:all]',$query);				
        $add_attr = array();
        if($result) {
	        $cat_attr = array();
	        $parent_attr = array();
	        $cat_name_array = array();
	        foreach($result as $value) {
		        if($value['attribute']['cat_id']==$cat_id) {
			        $cat_attr[] = $value['attribute'];
			        $cat_name_array[] = $value['attribute']['attr_name'];
		        } elseif($value['attribute']['cat_id']==$parent_id) {
			        $parent_attr[] = $value['attribute'];
		        }
	        }
	        unset($result);
	        if($cat_attr && $parent_attr) {
		        foreach($parent_attr as $value) {
			        if(!in_array($value['attr_name'],$cat_name_array)) {
				        $add_attr[] = $value;
			        }
		        }
	        } else {
		        $add_attr = $parent_attr;
	        }
	        unset($cat_attr,$parent_attr,$cat_name_array);
	        if($add_attr) {
		        $return_array = array();
		        $i = 0;
		        foreach($add_attr as $key=>$value) {
			        $post['attribute']['cat_id'] = $cat_id;
			        $post['attribute']['attr_name'] = $value['attr_name'];
			        $post['attribute']['input_type'] = $value['input_type'];
			        $post['attribute']['attr_values'] = $value['attr_values'];
			        $post['attribute']['sort_order'] = $value['sort_order'];
					$new_attr_id = POP::exe('[@@:UM attribute][@@:QU save]',$post);					
			        if($new_attr_id) {
				        $return_array[$key]['attr_id'] = $new_attr_id;
				        $return_array[$key]['cat_id'] = $cat_id;
				        $return_array[$key]['attr_name'] = iconv('GB2312','UTF-8' , $value['attr_name']);
				        $return_array[$key]['input_type'] = $value['input_type'];
				        $return_array[$key]['attr_values'] = iconv('GB2312','UTF-8' , $value['attr_values']);
				        $return_array[$key]['sort_order'] = $value['sort_order'];
				        $i++;
			        }
		        }
		        echo json_encode($return_array);
	        } else {
		        exit("-2");
	        }
        } else {
	        exit("-2");
        }
        exit;		
	}	
	
	public function categorieslist() {
		$query = array(
			'order' => ' sort_order asc,id asc ', 
			'fields' => array('goodscategories.*'), 
		);
		$result_category = POP::exe('[@@:UM goodscategories][@@:QU find:all]',$query);		
		$category_dg = $this->get_dg_category($result_category);
        $this->set('category_dg',$category_dg);			
	}	
	public function categoriesadd() {	
		if (!empty($this->data)) {		
			$query = $this->data;
			$insertid = POP::exe('[@@:UM goodscategories][@@:QU save]',$query);
			if ($insertid) {
				foreach($this->data['brand'] as $k=>$v) {
					$safeData = array();
					$safeData['brandgoodscategories']['brand_id'] = $v['brand_id'];
					$safeData['brandgoodscategories']['category_id'] = $insertid; 
					POP::exe('[@@:UM brandgoodscategories][@@:QU save]',$safeData);
				}	
				$this->flash('更新成功！', Router::url('admin/goods/categorieslist'));		
			} else {
				$this->flash('更新失败！', Router::url('admin/goods/categorieslist'));
			}				
				
	    } else {
			$query = array(
				'fields' => array('brand.*'), 
			);
			$brand_list = POP::exe('[@@:UM brand][@@:QU find:all]',$query);			
			$query = array(
				'order' => ' sort_order asc,id asc ', 
				'fields' => array('goodscategories.*'), 
			);
			$result_category = POP::exe('[@@:UM goodscategories][@@:QU find:all]',$query);				
		    $category_dg = $this->get_dg_category_select($result_category);
            $this->set('category_dg',$category_dg);
            $this->set('brand_list',$brand_list);		
		}

	}

	public function getcatbrandlist() {	
        $cat_id = intval($_POST['cat_id']);
        if(!$cat_id) {
	        exit();
        }		
        header('Content-Type: text/html; charset='.Param::read('App.encoding'));	
		$query = array(
				'joins' => array(
					array('type'=>'left','table'=>'brandgoodscategories','conditions'=>array('brandgoodscategories.brand_id' =>'brand.id')),					
					),
				'fields' => array('brand.id'),
				'conditions' => array('brandgoodscategories.category_id' =>$cat_id),
				'group' => ' brand.id DESC '
				);	
	    $list = POP::exe('[@@:UM brand][@@:QU find:all]',$query);	
	    echo json_encode($list);
		exit;
	}		

	
	public function categoriesedit($id) {		
		if (!empty($this->data)) {		
			$query['keyid'] = $id;
			$safeData['goodscategories'] = $this->data;				
			$query['set'] = $safeData;			
			if(POP::exe('[@@:UM goodscategories][@@:QU save]',$query)) {
				$query = array('conditions'=>array('brandgoodscategories.category_id' => $id));			
				POP::exe('[@@:UM brandgoodscategories][@@:QU del]',$query);
				if(isset($this->data['brand']) && !empty($this->data['brand'])) {
					foreach($this->data['brand'] as $k=>$v) {
						$safeData = array();
						$safeData['brandgoodscategories']['brand_id'] = $v['brand_id'];
						$safeData['brandgoodscategories']['category_id'] = $id; 
					    POP::exe('[@@:UM brandgoodscategories][@@:QU save]',$safeData);						
					}
				}					
				$this->flash('更新成功！', Router::url('admin/goods/categorieslist'));		
			} else {
				$this->flash('更新失败！', Router::url('admin/goods/categorieslist'));
			}			 	
	    }else {
			$query = array(
				'order' => ' sort_order asc,id asc ', 
				'fields' => array('goodscategories.*'), 
			);
			$result_category = POP::exe('[@@:UM goodscategories][@@:QU find:all]',$query);			
		    $category_dg = $this->get_dg_category_select($result_category);
            foreach($result_category as $v) {
	            if($v['goodscategories']['id']==$id) {
		            $info = $v['goodscategories'];
	            }
            }
			$query = array(
				'fields' => array('brand.*'), 
			);
			$brand_list = POP::exe('[@@:UM brand][@@:QU find:all]',$query);			
			$query = array(
				'conditions' => array('brandgoodscategories.category_id' => $id), 
				'fields' => array('brandgoodscategories.*'), 
			);
			$list = POP::exe('[@@:UM brandgoodscategories][@@:QU find:all]',$query);				
            $category_brand_list=array();
            if (is_array($list)) {
	            foreach ($list as $value){
		            $category_brand_list[]=$value['brandgoodscategories']['brand_id'];
	            }
            }			
		    $this->set('info',$info);	
            $this->set('category_dg',$category_dg);	
            $this->set('brand_list',$brand_list);
			$this->set('category_brand_list',$category_brand_list);
		}
		
	}
	
	public function categoriesdel($id) {
		$query = array(
			'conditions' => array('goodscategories.parent_id' => $id), 
			'fields' => array('goodscategories.*'), 
		);
		$parent_id_last = POP::exe('[@@:UM goodscategories][@@:QU find:first]',$query);		
        if(isset($parent_id_last['goodscategories']['id'])) {
		    $this->flash('删除失败，请先删除该项的子项目！', Router::url('admin/goods/categorieslist/'));			 
		}else if (isset($parent_id_last['goodscategories']['goods_num']) && $parent_id_last['goodscategories']['goods_num']>0) {
		    $this->flash('删除失败，请先删除该分类的产品内容！', Router::url('admin/goods/categorieslist/'));
		}else {
			$query = array('keyid'=>$id);			
			POP::exe('[@@:UM goodscategories][@@:QU del]',$query);			
			$query = array('conditions'=>array('brandgoodscategories.category_id' => $id));			
			POP::exe('[@@:UM brandgoodscategories][@@:QU del]',$query);			
		    $this->flash('删除成功！', Router::url('admin/goods/categorieslist/'));
        }
	}
	
	public function getnbsp($i) {
	    $str = '';
	    if($i) {
		    for($j=0; $j<$i; $j++) {
			    $str .= "　";
		    }
	    }
	    return $str;
    }

	public function get_dg_category($array,$parentid=0,$level=0,$add=2) {
	$str_pad = $this->getnbsp($level);
	$newarray = array();
	$temp = array();
	foreach($array as $v) {
		if($v['goodscategories']['parent_id'] == $parentid) {
			$newarray[] = array(
				'id' => $v['goodscategories']['id'],
				'cat_name' => $v['goodscategories']['cat_name'],
				'parent_id' => $v['goodscategories']['parent_id'],
				'goods_num' => $v['goodscategories']['goods_num'],
				'sort_order' => $v['goodscategories']['sort_order'],
				'str_pad' => $str_pad
			);
			$temp = $this->get_dg_category($array,$v['goodscategories']['id'],($level+$add));
			if($temp) {
				$newarray = array_merge($newarray, $temp);
			}
		}
	}
	return $newarray;
    }	
	public function get_dg_category_select($array,$parentid=0,$level=0,$add=2) {
	$str_pad = $this->getnbsp($level);
	$newarray = array();
	$temp = array();
	foreach($array as $v) {
		if($v['goodscategories']['parent_id'] == $parentid) {
			$newarray[] = array(
				'id' => $v['goodscategories']['id'],
				'cat_name' => $v['goodscategories']['cat_name'],
				'parent_id' => $v['goodscategories']['parent_id'],
				'goods_num' => $v['goodscategories']['goods_num'],
				'sort_order' => $v['goodscategories']['sort_order'],
				'str_pad' => $str_pad
			);
			//$temp = get_dg_category($array,$v['cat_id'],($level+$add));
			if($temp) {
				$newarray = array_merge($newarray, $temp);
			}
		}
	}
	return $newarray;
    }
	
	

	
}
function remind_info_replace($tpl,$array) {
	$arr1 = $arr2 = array();
	foreach($array as $k=>$v) {
		$arr1[] = '{'.$k.'}';
		$arr2[] = $v;
	}
	return str_replace($arr1,$arr2,$tpl);
}
?>